
var aux = 'ok';
Ext.define('administracion.UsuariosForm', {
    extend: 'Ext.form.Panel',
    alias: 'widget.UsuariosForm',
    
    requires: ['Ext.form.field.Text'],

    initComponent: function(){
        Ext.apply(this, {
            activeRecord: null,
            iconCls: 'icon-user',
            frame: true,
            title: 'Usuarios',
            defaultType: 'textfield',
            bodyPadding: 15,
            fieldDefaults: {
                anchor: '100%',
            },
            items: [{
                fieldLabel: 'Id',
                hidden: true,
                name: 'id',
                type: 'int',
            },{
                fieldLabel: '<b>Nombre Usuario</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'nombreUsuario',
                itemId: 'f_nombreUsuario',              
				
            },{
                fieldLabel: '<b>Nombre</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'nombre',
                itemId: 'f_nombre',
            }, {
                fieldLabel: '<b>Apellido</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'apellido',
                itemId: 'f_apellido',
            }, {
                fieldLabel: '<b>Telefono</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'telefono',
                itemId: 'f_telefono',
                type: 'int',
            }, {
                fieldLabel: '<b>Nº Documento</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'nroDocumento',
                itemId: 'f_nroDocumento',
                type: 'int',
            },{
                fieldLabel: '<b>Email</b>',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                name: 'email',
                itemId: 'f_email',
                allowBlank: false,
                vtype: 'email'
            },{
                fieldLabel: '<b>Fecha Creacion</b>',
                name: 'fechaCreacion',
                fieldStyle: 'background-color: #fff; border: 1px dotted;',
                itemId: 'f_fechaCreacion',
                hidden: true,
            },{
                fieldLabel: '<b>Password</b>',
                name: 'password1',
                itemId: 'f_password1',
                fieldStyle: 'background-color: #fff; border: 1px dotted;',
                hidden: true,
				inputType: 'password',
            },{
                fieldLabel: '<b>Repetir Password</b>',
                name: 'password2',
                itemId: 'f_password2',
                fieldStyle: 'background-color: #fff; border: 1px dotted;',
                hidden: true,
				inputType: 'password',
            }],
            dockedItems: [
            {
				xtype: 'toolbar',
				items: [
				{
                    itemId: 'idinfo-buscar',
                    iconCls: 'icon-checked',
                    text: '<b><font color="red">Campos habilitados para filtrado</font></b>',
                    hidden: true,
                    scope: this,
                    handler: this.onReset
                    
                },{
                    iconCls: 'icon-save',
                    itemId: 'save',
                    text: '<b>Guardar</b>',
                    hidden: true,
                    scope: this, 
                    handler: this.onSave
                },{
                    iconCls: 'icon-user-add',
                    itemId: 'create',
                    text: '<b>Crear</b>',
                    hidden: true,
                    scope: this,
                    handler: this.onCreate
                },{
                    iconCls: 'icon-rol',
                    itemId: 'rol',
                    text: '<b>Roles</b>',
                    hidden: true,
                    disabled: true,
                    scope: this, 
                    handler: this.onRol
                }, {}]
			},
            {
				weight: 1,
                xtype: 'toolbar',
                dock: 'bottom',
                items: ['->',{
                    iconCls: 'icon-user-delete',
                    itemId: 'delete',
                    hidden: true,
                    disabled: true,
                    scope: this, 
                    text: '<b>Eliminar</b>',
                    scope: this,
                    handler: this.onDelete
                },{
                    iconCls: 'icon-editar',
                    itemId: 'editar',
                    text: '<b>Editar</b>',
                    disabled: true,
                    hidden: true,
                    scope: this, 
                    handler: this.onEditar
                },{
                    iconCls: 'icon-add',
                    itemId: 'nuevo',
                    text: '<b>Nuevo</b>',
                    scope: this,
                    handler: this.onNuevo
                }, {
                    iconCls: 'icon-buscar',
                    itemId: 'idbuscar',
                    text: '<b>Buscar</b>',
                    hidden: true,
                    scope: this,
                    handler: this.onBuscar
                }, {
                    iconCls: 'icon-reset',
                    text: '<b>Reset</b>',
                    scope: this,
                    handler: this.onReset
                }]
            }]
        });
        this.callParent();
    },

    setActiveRecord: function(record){
        this.activeRecord = record;
        if (record) {
            if (!this.down('#editar').isDisabled()){
				this.down('#editar').show();			
			}			
            if (!this.down('#delete').isDisabled()){
				this.down('#delete').show();			
			}
            if (!this.down('#rol').isDisabled()){
				this.down('#rol').show();			
			}			
            this.down('#nuevo').hide(); 
            this.down('#create').hide(); 
            this.down('#save').hide();
            this.down('#f_password1').hide(); 
            this.down('#f_password2').hide();
            this.down('#f_fechaCreacion').hide(); 
            this.down('#idbuscar').hide();
            this.down('#idinfo-buscar').hide();
            this.down('#f_nombre').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_nombreUsuario').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_apellido').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_nroDocumento').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_telefono').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_email').setFieldStyle('background-color: transparent; border: none; background-image: none;');
                       
            this.getForm().loadRecord(record);
        } else {
            this.down('#editar').hide();
            this.down('#delete').hide();
            this.down('#rol').hide();
            if (!this.down('#nuevo').isDisabled()){
				this.down('#nuevo').show();			
			} 
			this.down('#f_password1').hide();
			this.down('#f_password2').hide();
			this.down('#save').hide();			
			this.down('#idbuscar').hide();
			this.down('#idinfo-buscar').hide();
			this.down('#f_fechaCreacion').hide(); 
			this.down('#f_nombre').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_nombreUsuario').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_apellido').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_nroDocumento').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_telefono').setFieldStyle('background-color: transparent; border: none; background-image: none;');
			this.down('#f_email').setFieldStyle('background-color: transparent; border: none; background-image: none;');
            this.getForm().reset();
        }
    },

    onNuevo: function(){
		this.getForm().reset();
		this.down('#create').show();
		this.down('#nuevo').hide(); 
		this.down('#f_nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nombreUsuario').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_apellido').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nroDocumento').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_telefono').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_email').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_password1').show();
		this.down('#f_password2').show();

				
    },
    
    onEditar: function(){
		this.down('#rol').hide();
		this.down('#delete').hide();
		this.down('#editar').hide();
		this.down('#save').show();
		this.down('#f_nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nombreUsuario').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_apellido').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nroDocumento').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_telefono').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_email').setFieldStyle('background-color: #fff; border: 1px dotted;');
		
    },    
    
    onSave: function(){
        var form = this.getForm();

                    form.submit({
                        
                        url: 'modificarUsuarios',
                        params: {
								submit: 'submit'
						},
						method: "post",
                        success: function(form, action) {
								//form.reset();
								this.down('#f_nombre').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#f_nombreUsuario').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#f_apellido').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#f_nroDocumento').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#f_telefono').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#f_email').setFieldStyle('background-color: transparent; border: none; background-image: none;');
								this.down('#save').hide();
								this.down('#rol').show();
								this.down('#delete').show();
								this.down('#editar').show();
								
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Usuario modificado satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',

								});
							this.fireEvent('refresh');
							                       
                        },
                        scope: this,
                        failure: function(form, action) {
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido modificar el usuario', //<- El mensaje  
										icon: 'ext-mb-error',

									});
                        }
                    });

    },

    onCreate: function(){
        var form = this.getForm();

                    form.submit({
                        
                        url: 'crearUsuarios',
                        params: {
								submit: 'submit'
						},
						method: "post",
                        success: function(form, action) {
								form.reset();
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Usuario creado satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',

								});
							this.fireEvent('refresh');
							                       
                        },
                        scope: this,
                        failure: function(form, action) {
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido crear el usuario', //<- El mensaje  
										icon: 'ext-mb-error',

									});
                        }
                    });


    },

    onDelete: function(){ 
        var form = this.getForm();

                    form.submit({
                        
                        url: 'eliminarUsuarios',
                        params: {
								submit: 'submit'
						},
						method: "post",
                        success: function(form, action) {
								Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Usuario eliminado satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',

								});
							this.fireEvent('refresh');
							this.onReset();
							                       
                        },
                        scope: this,
                        failure: function(form, action) {
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido eliminar el usuario', //<- El mensaje  
										icon: 'ext-mb-error',

									});
                        }
                    });


    },
	onRol: function(){
			var form = this.getForm();
			this.fireEvent('roles', form.getValues(true));
            
	},

    onReset: function(){
        this.setActiveRecord(null);
        this.getForm().reset();
        this.fireEvent('reset'); 
    },
    
    onFiltrar: function(){
		this.getForm().reset();
		this.down('#nuevo').hide();
		this.down('#rol').hide();
		this.down('#delete').hide();
		this.down('#editar').hide();
		this.down('#create').hide();
		this.down('#f_password1').hide(); 
		this.down('#f_password2').hide(); 
		this.down('#idbuscar').show();
		this.down('#idinfo-buscar').show();
		this.down('#f_fechaCreacion').show(); 
		this.down('#f_nombre').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nombreUsuario').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_apellido').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_nroDocumento').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_telefono').setFieldStyle('background-color: #fff; border: 1px dotted;');
		this.down('#f_email').setFieldStyle('background-color: #fff; border: 1px dotted;');
						
    },
    
    onBuscar: function(){
		var form = this.getForm();
		this.fireEvent('buscar', form.getValues(true));
						
    },
    onListar: function(){
		this.fireEvent('listar');
						
    },
      
  
    
});
